package WebRoot.demo4.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClusHandle {

	
	/**
	 * 查詢數據
	 * @param startidx
	 * @param endidx
	 * @param _list
	 * @return
	 */
	public static ArrayList<Object> LimitResult(long startidx, long endidx, List<Clus_count> _list) {

		ArrayList<Object> list = new ArrayList<>();
		
		if (startidx>=endidx) {
			System.out.println("startidx 必须比 endidx小");
			return list;
		}
		
		System.out.println("-----------");
		System.out.println("查集群开始=" + startidx + "   结束" + endidx+"  总共数量="+(endidx-startidx+1));

		long sum = 0; // 累计查找到数据总数
		long length = endidx - startidx+1; // 总共要查的总数
		long findnum = 0; // 已查询过的数据

		for (int i = 0; i < _list.size(); i++) {

			Clus_count cc = _list.get(i);
			if (startidx+1-findnum>cc.getCountcols()) {
				findnum = findnum + cc.getCountcols();
				continue;
			}

			if ((length - sum) <= (cc.getCountcols()-(startidx-findnum))) {
				// 该分区数据已足够，直接输出
				if (sum > 0) {
					list.addAll(singleClusResult(cc, 0, (length - sum - 1L)));
				} else {
					list.addAll(singleClusResult(cc, (startidx-findnum), (startidx-findnum+length - sum-1L)));
				}
				break;
			}

			// 分区数据不够,先获取该分区数据
			if (sum > 0) {
				list.addAll(singleClusResult(cc, 0, (cc.getCountcols() - 1L)));
				sum = sum + cc.getCountcols();
				findnum = findnum + cc.getCountcols();
			} else {
				list.addAll(singleClusResult(cc,(startidx-findnum),(cc.getCountcols() - 1L)));
				sum = sum + cc.getCountcols()-(startidx-findnum);
				findnum = findnum + cc.getCountcols();
			}

		}
		return list;
	}
	
	/**
	 * 查詢數據
	 * @param cc
	 * @param startidx
	 * @param endidx
	 * @return
	 */
	private static ArrayList<Object> singleClusResult(Clus_count cc,long startidx,long endidx) {
		if (startidx<0||endidx<0) {
			System.err.println("startidx or endidx 不能小于0");
		}
		
		if (startidx>endidx) {
			System.err.println("startidx 不能大于 endidx ");
		}
		
		System.out.println("key=" + cc.getKeyscol()
				+ "   start_clus_start_idx=" + startidx
				+ "   start_clus_end_idx=" + endidx+"  数量为"+(endidx-startidx+1));
		
		return cc.getCluList(startidx, endidx);
	}
	
	/**
	 * 查詢數據
	 * @param startidx
	 * @param endidx
	 * @param _list
	 * @return
	 */
	public static Map<String, Map<String, Long>> LimitCount(long startidx, long endidx, List<Clus_count> _list) {

		Map<String, Map<String, Long>> allMap = new HashMap();
		
		if (startidx>=endidx) {
			System.out.println("startidx 必须比 endidx小");
			return null;
		}
		
		System.out.println("-----------");
		System.out.println("查集群开始=" + startidx + "   结束" + endidx+"  总共数量="+(endidx-startidx+1));

		long sum = 0; // 累计查找到数据总数
		long length = endidx - startidx+1; // 总共要查的总数
		long findnum = 0; // 已查询过的数据

		for (int i = 0; i < _list.size(); i++) {

			Clus_count cc = _list.get(i);
			if (startidx+1-findnum>cc.getCountcols()) {
				findnum = findnum + cc.getCountcols();
				continue;
			}

			if ((length - sum) <= (cc.getCountcols()-(startidx-findnum))) {
				// 该分区数据已足够，直接输出
				if (sum > 0) {
					Map<String, Long> map = singleCluCount(cc, 0, (length - sum - 1L));
					allMap.put(cc.getKeyscol(), map);
				} else {
					Map<String, Long> map = singleCluCount(cc, 0, (length - sum - 1L));
					allMap.put(cc.getKeyscol(), map);
				}
				break;
			}

			// 分区数据不够,先获取该分区数据
			if (sum > 0) {
				Map<String, Long> map = singleCluCount(cc, 0, (length - sum - 1L));
				allMap.put(cc.getKeyscol(),  map);
				sum = sum + cc.getCountcols();
				findnum = findnum + cc.getCountcols();
			} else {
				Map<String, Long> map = (singleCluCount(cc,(startidx-findnum),(cc.getCountcols() - 1L)));
				allMap.put(cc.getKeyscol(),  map);
				sum = sum + cc.getCountcols()-(startidx-findnum);
				findnum = findnum + cc.getCountcols();
			}

		}
		return allMap;
	}
	
	/**
	 * 查詢數量
	 * @param cc
	 * @param startidx
	 * @param endidx
	 * @return
	 */
	private static Map<String, Long> singleCluCount(Clus_count cc,long startidx,long endidx) {
		
		Map<String, Long> map = new HashMap<>();
		
		if (startidx<0||endidx<0) {
			System.err.println("startidx or endidx 不能小于0");
			return null;
		}
		
		if (startidx>endidx) {
			System.err.println("startidx 不能大于 endidx ");
			return null;
		}
		
		System.out.println("key=" + cc.getKeyscol()
				+ "   start_clus_start_idx=" + startidx
				+ "   start_clus_end_idx=" + endidx+"  数量为"+(endidx-startidx+1));
		
		map.put("startidx", startidx);
		map.put("endidx", endidx);
		
		return map;
	}
	
}
